package cn.pku.eecs.ldn.activity;

import java.util.List;

import cn.pku.eecs.ldn.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class RecognizerIntentExampleActivity extends Activity{
	private static final int REQUEST_CODE_RECOGNIZER = 1;
	
	EditText editText = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_recognizer_intent_example);
		
		editText = (EditText) findViewById(R.id.editText1); 
		
		Button button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				goToRecognizer();
			}
		});
	}
	
	private void goToRecognizer() {
		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "this is a prompt");
		startActivityForResult(intent, REQUEST_CODE_RECOGNIZER);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch(requestCode) {
		case REQUEST_CODE_RECOGNIZER:
			if(resultCode == RESULT_OK) {
				List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
				editText.setText(results.toString());
			}
			break;
		default:
			break;
		}
	}
}
